package com.fw.service.persistence.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;

import com.fw.service.persistence.ServiceProfilePersistenceService;
import com.fw.service.persistence.dao.ServiceProfileDAO;
import com.fw.service.persistence.dto.ServiceProfile;

public class ServiceProfilePersistenceServiceImpl implements ServiceProfilePersistenceService {

    @Autowired
    private ServiceProfileDAO serviceProfileDAO;

    @Override
    public List<ServiceProfile> selectByServiceIdAndStatus(final String serviceId, final String serviceStatus) {

	Specification<ServiceProfile> specification = new Specification<ServiceProfile>() {
	    @Override
	    public Predicate toPredicate(Root<ServiceProfile> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (serviceId != null && !serviceId.isEmpty()) {
		    predicates.add(cb.like(root.<String> get("serviceId"), serviceId));
		}
		if (serviceStatus != null && !serviceStatus.isEmpty()) {
		    predicates.add(cb.equal(root.<String> get("serviceStatus"), serviceStatus));
		}
		Predicate result = cb.conjunction();
		for (Predicate predicate : predicates) {
		    result.getExpressions().add(predicate);
		}
		return result;
	    }
	};
	return serviceProfileDAO.findAll(specification);
    }

    @Override
    public List<ServiceProfile> selectByStatus(final String serviceStatus) {
	Specification<ServiceProfile> specification = new Specification<ServiceProfile>() {
	    @Override
	    public Predicate toPredicate(Root<ServiceProfile> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (serviceStatus != null && !serviceStatus.isEmpty()) {
		    predicates.add(cb.equal(root.<String> get("serviceStatus"), serviceStatus));
		}
		Predicate result = cb.conjunction();
		for (Predicate predicate : predicates) {
		    result.getExpressions().add(predicate);
		}
		return result;
	    }
	};
	return serviceProfileDAO.findAll(specification);
    }

    @Override
    public ServiceProfile save(ServiceProfile serviceProfile) {
	return serviceProfile == null ? null : serviceProfileDAO.save(serviceProfile);
    }

    @Override
    public ServiceProfile findOne(Long id) {
	return id == null ? null : serviceProfileDAO.findOne(id);
    }

    @Override
    public Page<ServiceProfile> findAll(Specification<ServiceProfile> specification, PageRequest pageRequest) {
	return specification == null || pageRequest == null ? null : serviceProfileDAO.findAll(specification, pageRequest);
    }

    @Override
    public List<ServiceProfile> selectByMerchantIdAndNotStatus(final String merchantId, final String serviceStatus) {
	Specification<ServiceProfile> specification = new Specification<ServiceProfile>() {
	    @Override
	    public Predicate toPredicate(Root<ServiceProfile> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (merchantId != null && !merchantId.isEmpty()) {
		    predicates.add(cb.like(root.<String> get("merchantId"), merchantId));
		}
		if (serviceStatus != null && !serviceStatus.isEmpty()) {
		    predicates.add(cb.notEqual(root.<String> get("serviceStatus"), serviceStatus));
		}
		Predicate result = cb.conjunction();
		for (Predicate predicate : predicates) {
		    result.getExpressions().add(predicate);
		}
		return result;
	    }
	};
	return serviceProfileDAO.findAll(specification, new Sort(Direction.ASC, "serviceId"));
    }

    @Override
    public List<Object> selectDistinctServiceId() {
	return serviceProfileDAO.selectDistinctServiceId();
    }

    @Override
    public List<Object> selectDistinctMerchantId() {
	return serviceProfileDAO.selectDistinctMerchantId();
    }

    @Override
    public List<Object> selectDistinctMerchantName() {
	return serviceProfileDAO.selectDistinctMerchantName();
    }

    @Override
    public List<Object> selectDistinctCompanyName() {
	return serviceProfileDAO.selectDistinctCompanyName();
    }

    @Override
    public List<Object> selectDistinctMdr() {
	return serviceProfileDAO.selectDistinctMdr();
    }

    @Override
    public List<Object> selectDistinctServiceFee() {
	return serviceProfileDAO.selectDistinctServiceFee();
    }

    @Override
    public List<Object> selectDistinctContact1Name() {
	return serviceProfileDAO.selectDistinctContact1Name();
    }

}
